package com.learn.demo.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

public class NIOFileCopyDemoClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 7001));
        File file = new File("C:\\Users\\Ban&Deng\\Downloads\\QQPCDownload1652.exe");
        FileChannel fileChannel = new FileInputStream(file).getChannel();
        long startTime = System.currentTimeMillis();
        //注意系统区别，Linux服务器该方法一次搞定，Windows系统一次8M
        long transferToCount = fileChannel.transferTo(0, file.length(), socketChannel);
        System.out.println(String.format("发送总字节%d,耗时%dms", transferToCount, System.currentTimeMillis() - startTime));
        fileChannel.close();
        socketChannel.close();
    }
}
